<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="public/css.css"> 

<h1>Nguyen Van Khanh </h1>
<?php
require_once "model/db.php";
$query = "SELECT * FROM pages";
$pages = getpages($query);

$query = "SELECT * FROM companies";
$companies = getpages($query);
?>
<form method="POST">
    Min Likes :  <input type="text" name="Min"><br>
    Max Likes :  <input type="text" name="Max"><br>
    Keyword :    <input type="text" name="keyword"><br>
    Company:     <select name="id_company">
        <?php foreach ($companies as $company): ?>
            <option value="<?php echo $company ['id'] ?>"><?php echo $company['name'] ?></option>
        <?php endforeach; ?>
    </select>
    <input type="submit" name="submit" value="Search"></form>

<?php if (isset($_POST['submit'])): ?>
    <?php
    $min = (double) $_POST['Min'];
    $max = (double) $_POST['Max'];
    $keyword = $_POST['keyword'];
    $query = "SELECT * FROM pages
             WHERE (likes <= $max)
                      AND (likes >= $min)
                      AND (description LIKE '% $keyword %')
                  LIMIT 0, 10
                  ";
    $pages = getpages($query);
    ?>
    <?php if (!empty($pages)): ?>

        <table border ='2'>
            <tr>
                <th> Pid </th>
                <th> ID </th>
                <th> Picture </th>
                <th> Name </th>
                <th> User name </th>
                <th> Phone </th>
                <th> Likes </th>
                <th> Talking_About_Count </th>
                <th> Category</th>
                <th> Link </th>
                <th> Url </th>
                <th> Website </th>
                <th> Street </th>
                <th> City </th>
                <th> Country </th>
            </tr>
            <?php foreach ($pages as $page): ?> 

                <tr>
                    <td><?php echo $page["pid"] ?></td>
                    <td><?php echo $page["id"] ?></td>
                    <td><img src = '<?php echo $page["picture"] ?>'></td>
                    <td><?php echo $page["name"] ?></td>
                    <td><?php echo $page["username"] ?></td>
                    <td><?php echo $page["phone"] ?></td>
                    <td><?php echo $page["likes"] ?></td>
                    <td><?php echo $page["talking_about_count"] ?></td>
                    <td><?php echo $page["category"] ?></td>
                    <td><?php echo $page["link"] ?></td>
                    <td><?php echo $page["url"] ?></td>
                    <td><?php echo $page["website"] ?></td>
                    <td><?php echo $page["picture"] ?></td>
                    <td><?php echo $page["street"] ?></td>
                    <td><?php echo $page["city"] ?></td>
                    <td><?php echo $page["country"] ?></td>
                </tr>

            <?php endforeach; ?>
        </table>
    <?php endif; ?>
<?php endif; ?>
